//
//  ViewController.m
//  SlideViewTest
//
//  Created by Nancy on 13-7-11.
//  Copyright (c) 2013年 Nancy. All rights reserved.
//

#import "ViewController.h"
#import "model.h"
#import "CycleScrollView.h"
#import "NSTimer+Addition.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (nonatomic, retain) CycleScrollView *mainScorllView;

@property (nonatomic, retain) UIImageView *titleView;
@end


@implementation ViewController

//@synthesize viewDeckController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    ((UILabel *)self.navigationItem.titleView).text = title;
    [self.navigationItem.titleView sizeToFit];
}

#pragma mark --- Button Events

-(void)topButtonClickHandler:(id)sender
{
    [self.viewDeckController toggleTopViewAnimated:YES];
    
}

-(void)bottomButtonClickHandler:(id)sender
{
    [self.viewDeckController toggleBottomViewAnimated:YES];
    
}


-(void)leftButton:(id)sender
{
    model *mod=[model shareModel];
    mod.left=YES;
    [self.viewDeckController toggleLeftViewAnimated:YES];
}

-(void)rightButton:(id)sender
{
    model *mod=[model shareModel];
    mod.right=YES;
    [self.viewDeckController toggleRightViewAnimated:YES];
}

- (void)backButton
{
    model *mode=[model shareModel];
    if (mode.left) {
        [self.viewDeckController toggleLeftViewAnimated:YES];
    } else if (mode.right){
        [self.viewDeckController toggleRightViewAnimated:YES];
    }
}



- (void)viewWillAppear:(BOOL)animated
{
    model *mode=[model shareModel];
    if (mode.backHome) {
        UIImage *image1=[UIImage imageNamed:@"c1.png"];
        UIImageView *band1=[[UIImageView alloc] initWithImage:image1];
        band1.frame=CGRectMake(0, 0, image1.size.width, image1.size.height);
        //[self.view addSubview:band1];
        
        UIImage *image2=[UIImage imageNamed:@"c2.png"];
        UIImageView *band2=[[UIImageView alloc] initWithImage:image2];
        band2.frame=CGRectMake(image1.size.width, self.view.frame.size.height-image2.size.height, image2.size.width, image2.size.height);
        //[self.view addSubview:band2];
        
        UIImageView *band3=[[UIImageView alloc] initWithImage:image1];
        band3.frame=CGRectMake(image1.size.width*2, 0, image1.size.width, image1.size.height);
        //[self.view addSubview:band3];
        
        UIImageView *band4=[[UIImageView alloc] initWithImage:image2];
        band4.frame=CGRectMake(image1.size.width*3, self.view.frame.size.height-image2.size.height, image2.size.width, image2.size.height);
        //[self.view addSubview:band4];
        
        
        
        mode.backHome=NO;
    }
    
    
//    UIView *view=[[UIView alloc] initWithFrame:self.view.bounds];
//    view.backgroundColor=[UIColor whiteColor];
//    view.alpha=0.8;
//    [self.view addSubview:view];
//    
//    [UIView animateWithDuration:1.0 animations:^{
//        view.alpha=0;
//    } completion:^(BOOL finished) {
//        [view removeFromSuperview];
//    }];
    
//    [UIView animateWithDuration:0.8 animations:^{
//        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
//        
//    } completion:^(BOOL finished) {
//        [UIView animateWithDuration:0.5 animations:^{
//            self.view.alpha=1;
//        }];
//    }];
}

//- (void)updateViewConstraints
//{// 在这里为你的view添加约束，请确保该view的translatesAutoresizingMaskIntoConstraints属性已设置为NO
//    [super updateViewConstraints];
//    
//    self.autoLayoutDic=NSDictionaryOfVariableBindings(_titleView);
//    //    NSDictionary *metrics1 = @{@"hPadding":@5,@"vPadding":@5,@"imageEdge":@150.0};
//    UIImage *imageV=[UIImage imageNamed:@"t5.png"];
//    NSDictionary *metrics = @{@"left":@([self screenSize].width/2-imageV.size.width*6/24-10),@"right":@([self screenSize].width/2-imageV.size.width*6/24-10),@"width":@(imageV.size.width*6.3/12)};
//    NSString *vfl = @"|-left-[view(width)]-right-|";
//    //    NSString *vfl0 = @"V:|-25-[_boxV]";
//    //    NSString *vfl3 = @"V:|-vPadding-[_headerL]-vPadding-[_imageV(imageEdge)]-vPadding-[_backBtn]-vPadding-|";
//    [self.navigationController.navigationBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatAlignAllLeft metrics:metrics views:self.autoLayoutDic]];
//    
////    [self.navigationController.navigationBar addConstraint:[NSLayoutConstraint constraintWithItem:_titleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
//
//    
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"pingmukuandu=%f",[self screenSize].width);
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backButton)]];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self setNeedsStatusBarAppearanceUpdate];//状态栏实时更新
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];//R54 G176 B255
    [self.navigationController.navigationBar setTranslucent:YES];
    
//    CAGradientLayer *gradient = [CAGradientLayer layer];
//    gradient.frame = self.navigationController.navigationBar.frame;
//    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,
//                       (id)[UIColor yellowColor].CGColor,
//                       (id)[UIColor greenColor].CGColor,nil];
//    [self.navigationController.navigationBar.layer insertSublayer:gradient atIndex:10000];
    
    
    
//    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake([self screenSize].width/2-50, 13, 100, 15)];
//    titleLabel.text=@"全部职位";
//    titleLabel.textColor=[UIColor whiteColor];
//    titleLabel.textAlignment=NSTextAlignmentCenter;
//    titleLabel.font=[UIFont boldSystemFontOfSize:20];
//    [self.navigationController.navigationBar addSubview:titleLabel];
    
    
    self.autoLayoutDic=[[NSDictionary alloc] init];
#pragma mark - 标题
    UIImage *imageV=[UIImage imageNamed:@"t5@2x.png"];
    _titleView=[[UIImageView alloc] initWithImage:imageV];
    //_titleView.backgroundColor=[UIColor blueColor];
    if ([self screenSize].width==375.0) {
        _titleView.frame=CGRectMake([self screenSize].width/2-imageV.size.width*1.045/2, self.navigationController.navigationBar.frame.size.height/2-imageV.size.height/2-2, imageV.size.width*1.045, imageV.size.height);//6.3/12
    } else if ([self screenSize].width==320.0){
        _titleView.frame=CGRectMake([self screenSize].width/2-imageV.size.width*0.89/2, self.navigationController.navigationBar.frame.size.height/2-imageV.size.height/2-2, imageV.size.width*0.89, imageV.size.height);//6.3/12
    }
    _titleView.translatesAutoresizingMaskIntoConstraints=NO;
    [self.navigationController.navigationBar addSubview:_titleView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"Left" forState:UIControlStateNormal];
    leftBtn.titleLabel.textColor=[UIColor whiteColor];
    [leftBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    leftBtn.frame=CGRectMake(5, 8, 80, 15);
    [leftBtn addTarget:self action:@selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc] initWithCustomView:leftBtn];
//    self.navigationItem.leftBarButtonItem=leftBarBtn;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"Right" forState:UIControlStateNormal];
    rightBtn.titleLabel.textColor=[UIColor whiteColor];
    [rightBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    rightBtn.frame=CGRectMake([self screenSize].width-5-80, 8, 80, 15);
    [rightBtn addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightBarBtn=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
//    self.navigationItem.rightBarButtonItem=rightBarBtn;
    
    
#pragma  mark - 滚动栏
    if ([self screenSize].width==375) {
        self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(5, 70, [self screenSize].width-10, 180) animationDuration:2];
    } else if ([self screenSize].width==320){
        self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(5, 70, [self screenSize].width-10, 320*180/375) animationDuration:2];
    }
    
    self.mainScorllView.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
    
    NSMutableArray *viewsArray = [@[] mutableCopy];
    
    //NSArray *colorArray = @[[UIColor cyanColor],[UIColor blueColor],[UIColor greenColor]];
    for (int i = 0; i < 3; i++) {
//        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [self screenSize].width, 150)];
//        tempLabel.backgroundColor = [(UIColor *)[colorArray objectAtIndex:i] colorWithAlphaComponent:0.5];
//        [viewsArray addObject:tempLabel];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [self screenSize].width, self.mainScorllView.frame.size.height)];
        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"P%d.jpg",i+1]];
        [viewsArray addObject:imageView];
    }
    
    
    
    self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    self.mainScorllView.totalPagesCount = ^NSInteger(void){
        return 3;
    };
    self.mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
        NSLog(@"点击了第%ld个",(long)pageIndex);
    };
    self.mainScorllView.layer.masksToBounds=YES;
    self.mainScorllView.layer.cornerRadius=10;

    [self.view addSubview:self.mainScorllView];
    
    
    //分割线1
    UIView *line1=[[UIView alloc] initWithFrame:CGRectMake(0, self.mainScorllView.frame.origin.y+CGRectGetHeight(self.mainScorllView.frame)+7.5, [self screenSize].width, 0.5)];
    line1.backgroundColor=[UIColor colorWithRed:202.0/255.0 green:202.0/255.0 blue:202.0/255.0 alpha:1];
    [self.view addSubview:line1];
    
#pragma mark - 四个按钮
    for (int i=0; i<4; i++) {
        UIButton *button=[[UIButton alloc] init];
        if ([self screenSize].width==375) {
            button.frame=CGRectMake([self screenSize].width/2-(280+60)/2+(70+20)*i, self.mainScorllView.frame.origin.y+CGRectGetHeight(self.mainScorllView.frame)+15, 70, 70);
        } else if ([self screenSize].width==320){
            button.frame=CGRectMake([self screenSize].width/2-(240+45)/2+(60+15)*i, self.mainScorllView.frame.origin.y+CGRectGetHeight(self.mainScorllView.frame)+15, 60, 60);
        }
        switch (i) {
            case 0:{
                [button setBackgroundImage:[UIImage imageNamed:@"Q1.png"] forState:UIControlStateNormal];
            }
                break;
            case 1:{
                [button setBackgroundImage:[UIImage imageNamed:@"Q2.png"] forState:UIControlStateNormal];
            }
                break;
            case 2:{
                [button setBackgroundImage:[UIImage imageNamed:@"Q3.png"] forState:UIControlStateNormal];
            }
                break;
            case 3:{
                [button setBackgroundImage:[UIImage imageNamed:@"Q4.png"] forState:UIControlStateNormal];
            }
                break;
            default:
                break;
        }
        
        [self.view addSubview:button];
    }
    
    //分割线2
    UIView *line2=[[UIView alloc] initWithFrame:CGRectMake(0, self.mainScorllView.frame.origin.y+CGRectGetHeight(self.mainScorllView.frame)+92.5, [self screenSize].width, 0.5)];
    line2.backgroundColor=[UIColor colorWithRed:202.0/255.0 green:202.0/255.0 blue:202.0/255.0 alpha:1];
    [self.view addSubview:line2];
    
#pragma mark - 信息列表
    UIImage *image=[UIImage imageNamed:@"t0.png"];
    self.tabelView=[model shareTableView];
    self.tabelView.frame=CGRectMake([self screenSize].width/2-image.size.width*7/24, self.mainScorllView.frame.origin.y+CGRectGetHeight(self.mainScorllView.frame)+59*2+10-20-10, image.size.width*7/12, [self screenSize].height-self.mainScorllView.frame.origin.y-CGRectGetHeight(self.mainScorllView.frame)+20-59*2-10+20+10);
    
    self.tabelView.delegate=self;
    self.tabelView.dataSource=self;
    self.tabelView.backgroundColor=[UIColor grayColor];
    self.tabelView.showsVerticalScrollIndicator=NO;
    self.tabelView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tabelView];
    
//    UIImageView *view=[[UIImageView alloc] initWithFrame:self.tabelView.frame];
//    view.image=[UIImage imageNamed:@"cell.png"];
    
//    [self.view addSubview:view];
    
//    UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [topBtn setTitle:@"ShowTopView" forState:UIControlStateNormal];
//    [topBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
//    topBtn.frame = CGRectMake((320-100)/2, 20, 100.0, 30.0);
//    [topBtn addTarget:self action:@selector(topButtonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:topBtn];
//    
//    UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [bottomBtn setTitle:@"ShowBottomView" forState:UIControlStateNormal];
//    [bottomBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
//    bottomBtn.frame = CGRectMake((320-130)/2, 460-100.0, 130.0, 30.0);
//    [bottomBtn addTarget:self action:@selector(bottomButtonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:bottomBtn];
    

}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellStr=@"cell";
    UILabel *label=nil;
    UILabel *detailLabel=nil;
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
        
        label=[[UILabel alloc] initWithFrame:CGRectMake(80, 10, cell.frame.size.width-60, cell.frame.size.height-10)];
        label.text=@"校园传单派发员，男女不限，120/天，必须是在校生";
        label.font=[UIFont boldSystemFontOfSize:14];
        //label.backgroundColor=[UIColor redColor];
        label.textColor=[UIColor colorWithRed:0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
        label.lineBreakMode=NSLineBreakByCharWrapping;
        label.numberOfLines=3;
    
        detailLabel=[[UILabel alloc] initWithFrame:CGRectMake(80, label.frame.origin.y+label.frame.size.height+6, cell.frame.size.width-60, 9)];
        detailLabel.text=@"工资周结";
        detailLabel.font=[UIFont boldSystemFontOfSize:13];
        detailLabel.textColor=[UIColor colorWithRed:0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
        
    }
    
    UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"t1(2)@2x.png"]];
    UIImageView *imageView=[[UIImageView alloc] initWithImage:image];
    imageView.frame=CGRectMake(0, 3, image.size.width*1.2, image.size.height*1.4);//7.1/12,8.7/12
    [cell addSubview:imageView];
    
    [cell addSubview:label];
    [cell addSubview:detailLabel];
    
//    cell.textLabel.text=@"校园传单派发员，男女不限，120/天，必须是在校生";
//    cell.textLabel.numberOfLines=3;
//    cell.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
//    cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
//    cell.detailTextLabel.text=@"工资周结";
//    cell.imageView.image=[UIImage imageNamed:@"liebiao.jpg"];
//    cell.imageView.layer.masksToBounds=YES;
//    cell.imageView.layer.cornerRadius=25;

    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float x=scrollView.contentOffset.x;
    
    if (x>2*[self screenSize].width) {
        
        for (int i=100; i<103; i++) {
            [[scrollView viewWithTag:i] removeFromSuperview];
        }
        //调换广告位图片位置
        model *mod=[model shareModel];
        [mod.advArr exchangeObjectAtIndex:1 withObjectAtIndex:2];
        [mod.advArr exchangeObjectAtIndex:0 withObjectAtIndex:1];
        
        for (int i=0; i<3; i++) {
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(i*[self screenSize].width, 0, [self screenSize].width, 150)];
            view.tag=100+i;
            view.backgroundColor=[mod.advArr objectAtIndex:i];
            [scrollView addSubview:view];
        }
        
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    } else if (x<0){
        
        for (int i=100; i<103; i++) {
            [[scrollView viewWithTag:i] removeFromSuperview];
        }
        //调换广告位图片位置
        model *mod=[model shareModel];
        [mod.advArr exchangeObjectAtIndex:0 withObjectAtIndex:1];
        [mod.advArr exchangeObjectAtIndex:1 withObjectAtIndex:2];
        
        for (int i=0; i<3; i++) {
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(i*[self screenSize].width, 0, [self screenSize].width, 150)];
            view.tag=100+i;
            view.backgroundColor=[mod.advArr objectAtIndex:i];
            [scrollView addSubview:view];
        }
        
        [scrollView setContentOffset:CGPointMake(2*[self screenSize].width, 0) animated:NO];
    }
}

- (void)scroll
{
//    float x=self.scrollView.contentOffset.x+[self screenSize].width;
//    float y=self.scrollView.contentOffset.y;
    
}

//获取ios屏幕大小（去掉状态条）
- (CGSize) screenSize
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
